package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.PermissionDTO;
import com.cskaoyan.wordmemorize.dto.admin.RoleDTO;
import com.cskaoyan.wordmemorize.dto.admin.RolePermissionDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminRoleController {
    
    @Autowired
    RolePermissionService rolePermissionService;


    @GetMapping("/permission/list")
    public Result<List<PermissionDTO>> getPermissions() {

        List<PermissionDTO> permissionList = rolePermissionService.getPermissions();
        return Result.ok(permissionList);
    }


    @PutMapping("/permission")
    public Result updatePermission(PermissionCommand command) {

        rolePermissionService.updatePermission(command);
        return Result.ok();
    }

    @PostMapping("/permission")
    public Result savePermission(@RequestBody PermissionCommand command) {

        rolePermissionService.savePermissions(command);


        return Result.ok();
    }

    @DeleteMapping("/permission/{permissionId}")
    public Result deletePermission(@PathVariable Long permissionId) {

        rolePermissionService.deletePermission(permissionId);
        return Result.ok();
    }
    
    @PostMapping("role")
    public Result addRole(@RequestBody RoleCommand roleCommand) {
        
        rolePermissionService.addRole(roleCommand);
        
        return Result.ok();
    }
    
    @PutMapping("role")
    public Result updateRole(@RequestBody RoleCommand roleCommand) {
        
        rolePermissionService.updateRole(roleCommand);
        
        return Result.ok();
    }
    
    @PutMapping("/role/permission")
    public Result updateRolePermission(@RequestBody RolePermissionCommand rolePermissionCommand) {
        
        rolePermissionService.updateRolePermission(rolePermissionCommand);
        
        return Result.ok();
    }
    
    @GetMapping("/{roleId}/permission/list")
    public Result<RolePermissionDTO> getPermissionByRoleId(@PathVariable("roleId") Long roleId) {

        RolePermissionDTO rolePermissionList = rolePermissionService.getRolePermissionList(roleId);

        return Result.ok(rolePermissionList);
    }
    
    @GetMapping("/role/list")
    public Result<PageDTO<RoleDTO>> roleList(PageRequest pageRequest) {

        PageDTO<RoleDTO> roleList = rolePermissionService.getRoleList(pageRequest);

        return Result.ok(roleList);
    }
    
    @DeleteMapping("/role/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId) {

        rolePermissionService.deleteRole(roleId);

        return Result.ok();
    }
    
    @DeleteMapping("/permission/{roleId}/{permissionId}")
    public Result<RolePermissionDTO> deleteRolePermission(@PathVariable("roleId") Long roleId, @PathVariable("permissionId") Long permissionId) {
        //删除权限
        rolePermissionService.deleteRolePermission(roleId, permissionId);
        
        //获取删除之后的权限
        RolePermissionDTO rolePermissionList = rolePermissionService.getRolePermissionList(roleId);
        
        return Result.ok(rolePermissionList);
    }

    
}
